package com.diligreat.action.base;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



/** 
 * @author  Bruce 
 * @date 2011-4-4
 * @comment
 */
public class BaseAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1281824092820429037L;
	public static final String NO_AUTHORITY = "noAuthority";
	private String basePath = null;
	private HttpServletRequest request = null;
	private HttpServletResponse response = null;
	@SuppressWarnings("unused")
	private Map<String, Object> session = null;
	private ServletContext servletContext;
	
	
	public BaseAction(){
		if(basePath == null){
			String context = getRequest().getContextPath();   
			basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+context+"/";  
		}
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
	@Override
	public String input() throws Exception {
		
		return super.input();
	}

	
	@Override
	public void validate() {
		
		super.validate();
	}

	public HttpServletRequest getRequest(){
		request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return request;
	}
	
	public HttpServletResponse getResponse(){
		response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		return response; 
	}
	/**
	 * 获得工程绝对路径
	 * @return
	 */
	public String getBaseRealPath(){
		servletContext = ServletActionContext.getServletContext();
		return servletContext.getRealPath("/"); 
	}
	public Map<String, Object> getSession(){
		return ActionContext.getContext().getSession();
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getBasePath() {
		return basePath;
	}

}
